package com.mk.es.action;


import com.mk.es.service.entity.EsQueryParam;
import com.mk.es.service.entity.imp.ArticleEntity;
import com.mk.es.service.service.IArticleService;
import com.mk.es.service.transport.entity.EsQueryResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 *  ES资讯文章action
 */
@RestController("/article")
@RequestMapping("/article")
public class ArticleAction {
	
	@Resource(name="articleService")
    IArticleService articleService;

	/**
     *  提交文章索引信息
	 * @return
	 */
	@RequestMapping(value = "/addArticle")
	public Object addArticle() {
        ArticleEntity article = new ArticleEntity();
        article.setId("123");
        article.setTitle("标题");
        article.setSubTitle("副标题");
        article.setBrief("简介");
        article.setSource("来源");
        article.setTags("标签");
        article.setAuthor("作者");
        article.setContent("内容实体，这个是很好的文章");
        article.setCreateTime(System.currentTimeMillis());

        //添加索引数据
        articleService.addArticle(article);

        return "添加成功";
	}

    /**
     *  全量更新文章索引信息
     * @return
     */
    @RequestMapping(value = "/updateArticle")
    public Object updateArticle() {
        ArticleEntity article = new ArticleEntity();
        article.setId("123");
        article.setTitle("标题");
        article.setSubTitle("副标题");
        article.setBrief("简介");
        article.setSource("来源");
        article.setTags("标签");
        article.setAuthor("作者");
        article.setContent("内容实体，这个是很好的文章");
        article.setCreateTime(System.currentTimeMillis());

        //添加索引数据
        articleService.updateArticle(article);

        return "更新成功";
    }

    /**
     * 删除文章索引信息
     * @return
     */
    @RequestMapping(value = "/delArticle")
    public Object delArticle(String id) {
        if (id == null) {
            return "id为空";
        }

        //删除索引数据
        articleService.delArticle(id);

        return "删除成功";
    }

    /**
     * 查询文章索引信息
     * pageIndex
     * pageSize
     * queryStr
     * @param param
     * @return
     */
    @RequestMapping(value = "/queryArticle", method = RequestMethod.POST)
    public Object queryArticle(@RequestBody EsQueryParam param) {
        //查询文章
        EsQueryResult<ArticleEntity> esQueryResult =  articleService.queryArticle(param);

        return esQueryResult;
    }

}
